﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharePointPowerToys
{
    public class TextBoxSharePointEditorPartControl : Control, INamingContainer
    {
        public string Header { get; set; }
        public string Body { get; set; }
        public bool ShowFieldSeparator { get; set; }

        private TextBox textBox = new TextBox { CssClass = "UserInput", Width = 176 };

        public string Text
        {
            get
            {
                return textBox.Text;
            }

            set
            {
                textBox.Text = value;
            }
        }

        public TextBoxMode TextBoxMode
        {
            get
            {
                return textBox.TextMode;
            }
            set
            {
                textBox.TextMode = value;
            }
        }

        public bool BuilderVisible { get; set; }

        public TextBoxSharePointEditorPartControl()
        {
            Header = string.Empty;
            Body = string.Empty;
            TextBoxMode = TextBoxMode.SingleLine;
            BuilderVisible = false;
        }

        public TextBoxSharePointEditorPartControl(string id)
        {
            ID = id;
            Header = string.Empty;
            Body = string.Empty;
            TextBoxMode = TextBoxMode.SingleLine;
            BuilderVisible = false;
        }

        public TextBoxSharePointEditorPartControl(string id, string header)
        {
            ID = id;
            Header = header;
            Body = string.Empty;
            TextBoxMode = TextBoxMode.SingleLine;
            BuilderVisible = false;
        }

        public TextBoxSharePointEditorPartControl(string id, string header, string body)
        {
            ID = id;
            Header = header;
            Body = body;
            TextBoxMode = TextBoxMode.SingleLine;
            BuilderVisible = false;
        }

        public TextBoxSharePointEditorPartControl(string id, string header, string body, TextBoxMode textBoxMode)
        {
            ID = id;
            Header = header;
            Body = body;
            TextBoxMode = textBoxMode;
            BuilderVisible = false;
        }

        public TextBoxSharePointEditorPartControl(string id, string header, string body, TextBoxMode textBoxMode, bool builderVisible)
        {
            ID = id;
            Header = header;
            Body = body;
            TextBoxMode = TextBoxMode.SingleLine;
            BuilderVisible = builderVisible;
        }

        protected override void CreateChildControls()
        {
            try
            {
                //Controls.Add(new LiteralControl("<tr>"));
                //Controls.Add(new LiteralControl("<td>"));
                if (!String.IsNullOrEmpty(Header))
                {
                    Controls.Add(new LiteralControl("<div class=\"UserSectionHead ms-bold\">"));
                    Controls.Add(new LiteralControl(Header));
                    Controls.Add(new LiteralControl("</div>"));
                }

                Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
                Controls.Add(new LiteralControl(Body));

                Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
                Controls.Add(new LiteralControl("<nobr>"));

                textBox.ID = String.Concat(ID, CleanString(Header), CleanString(Body), "TextBox");
                Controls.Add(textBox);

                if (BuilderVisible)
                {
                    Button builderButton = new Button();
                    builderButton.Text = String.Concat(Header, "...");
                    Controls.Add(builderButton);
                }

                //textBox.TextMode = TextBoxMode;
                //Controls.Add(new LiteralControl(String.Concat("Password: ", Text)));

                Controls.Add(new LiteralControl("</nobr>"));
                Controls.Add(new LiteralControl("</div>"));
                Controls.Add(new LiteralControl("</div>"));

                if (ShowFieldSeparator)
                {
                    Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"></div>"));
                }
                //Controls.Add(new LiteralControl("</td>"));
                //Controls.Add(new LiteralControl("</tr>"));
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(String.Concat("Error: ", ex.Message)));
            }
        }

        private string CleanString(string value)
        {
            string results = value;
            results = results.Replace(" ", "");
            results = results.Replace(":", "");
            results = results.Replace("/", "");
            results = results.Replace("\\", "");
            results = results.Replace("[", "");
            results = results.Replace("]", "");
            results = results.Replace("(", "");
            results = results.Replace(")", "");
            return results;
        }
    }
}
